import webview


def open_confirmation_dialog(window):
    result = window.create_confirmation_dialog('问题', '您对这个内容满意吗?')
    if result:
        print('用户点击了确定')
    else:
        print('用户点击了取消')


def on_before_show(window):
    print('窗口即将显示，原生窗口对象：', window.native)


def on_closed():
    print('pywebview 窗口已关闭')


def on_closing(window):
    print('pywebview 窗口正在关闭')
    window.create_confirmation_dialog('提示', '应用已隐藏到托盘，你可以从托盘再次打开')
    return False


def on_shown():
    print('pywebview 窗口已显示')


def on_minimized():
    print('pywebview 窗口最小化')


def on_restored():
    print('pywebview 窗口已还原')


def on_maximized():
    print('pywebview 窗口最大化')


def on_resized(width, height):
    print(
        'pywebview 窗口尺寸更改。新尺寸为 {width} x {height}'.format(
            width=width, height=height
        )
    )


# 可选，可以通过窗口参数获取触发事件的窗口对象
def on_loaded(window):
    print('DOM已准备就绪')

    # 移除事件监听器
    window.events.loaded -= on_loaded
    window.load_url('https://pywebview.idepy.com/hello')


def on_moved(x, y):
    print('pywebview 窗口移动。新坐标为 x: {x}, y: {y}'.format(x=x, y=y))


if __name__ == '__main__':
    chinese = {
        'global.quitConfirmation': "确认退出"
    }
    window = webview.create_window(
        '简单浏览器', 'https://pywebview.idepy.com/',
        confirm_close=True, localization=chinese
    )

    window.events.closed += on_closed
    window.events.closing += on_closing
    window.events.before_show += on_before_show
    window.events.shown += on_shown
    window.events.loaded += on_loaded
    window.events.minimized += on_minimized
    window.events.maximized += on_maximized
    window.events.restored += on_restored
    window.events.resized += on_resized
    window.events.moved += on_moved

    webview.start(icon='assets/logo.ico')
